package main

import (
	"fmt"
	"os"
	"strconv"
)

/**
Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output:  321
Example 2:

Input: -123
Output: -321
Example 3:

Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

https://leetcode.com/problems/reverse-integer/description/

这个题要主要的3个点

* 如何翻转字符串
* 边界问题
* 考虑负数

1.如何翻转字符串
	要在不借助辅助堆栈/数组的情况下弹出和推入数字，我们可以使用数学的方法
	pop=x%10
	x/=10

	temp=rev*10+pop
	rev=temp

2.边界问题
32位整数最大值是2^31-1,最小值是-2^31
举个例子,2^31-1翻转后溢出,所以要考虑边界问题

3.考虑负数



时间复杂度: O(n)

https://stackoverflow.com/questions/3826704/why-int32-has-max-value-231-1

*/

const (
	MAX = 1<<31 - 1
	MIN = -MAX - 1
)

func reverse(x int) int {

	rev := 0
	for x != 0 {
		rev = rev*10 + x%10

		if rev > MAX || rev < MIN {
			return 0
		}

		x /= 10
	}

	return rev
}

func main() {
	if len(os.Args) != 2 {
		fmt.Fprintln(os.Stderr, "提示: 缺少输入")
		os.Exit(1)
	}
	intput, _ := strconv.Atoi(os.Args[1])
	fmt.Println(reverse(intput))
}
